package com.smec.moblie.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.bigkoo.alertview.AlertView;
import com.bigkoo.alertview.OnItemClickListener;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.smec.moblie.R;
import com.smec.moblie.activity.PhotoShowBigActivity;
import com.smec.moblie.bean.SelectListBean;
import com.smec.moblie.common.ConstValue;
import com.smec.moblie.control.EventCenter;

import org.litepal.crud.DataSupport;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import de.greenrobot.event.EventBus;

/**
 * Created by sunyubo on 2017/12/20.
 */

public class FeedBackImageAdapter extends BaseAdapter implements ConstValue {

    private List<SelectListBean> selectListBeanList;
    private LayoutInflater inflater;
    private Context context;
    private DisplayImageOptions options;
    private int feedBackPosition;
    private String status;
    private ImageLoader imageLoader;

    public FeedBackImageAdapter(Context context, int feedBackPosition, String status) {
        selectListBeanList = new ArrayList<>();
        this.feedBackPosition = feedBackPosition;
        inflater = LayoutInflater.from(context);
        this.status = status;
        this.context = context;
        options = new DisplayImageOptions.Builder().showStubImage(R.mipmap.photo).showImageForEmptyUri(R.mipmap.photo).showImageOnFail(R.mipmap.photo)
                .cacheInMemory(true).cacheOnDisc(true).bitmapConfig(Bitmap.Config.RGB_565) // 设置图片的解码类型
                .build();
    }

    public void setList(List<SelectListBean> selectListBeanList) {
        this.selectListBeanList = selectListBeanList;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return selectListBeanList.size();
    }

    @Override
    public SelectListBean getItem(int position) {
        return selectListBeanList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return selectListBeanList.get(position).getFeedbackId();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view;
        if (convertView == null) {
            view = inflater.inflate(R.layout.adapter_feedback_images, null);
        } else {
            view = convertView;
        }
        imageLoader = ImageLoader.getInstance();
        SelectListBean selectListBean = selectListBeanList.get(position);

        ImageView image_photo = (ImageView) view.findViewById(R.id.image_photo);
        ImageView image_add = (ImageView) view.findViewById(R.id.image_add);
        image_photo.setOnLongClickListener(new MyOnLongClickListener(position));
        image_photo.setOnClickListener(new MyOnClickListener(position));
        image_add.setOnClickListener(new MyOnClickListener(position));


        if ((selectListBean.getValue() != null && !selectListBean.getValue().equals("")) || (selectListBean.getImagePath() != null && !selectListBean.getImagePath().equals(ADDString))) {
            image_photo.setVisibility(View.VISIBLE);
            image_add.setVisibility(View.GONE);
            if (selectListBean.getValue() != null && !selectListBean.getValue().equals("")) {
                imageLoader.displayImage(selectListBean.getValue(), image_photo, options);
            } else {
                imageLoader.displayImage("file://" + selectListBean.getImagePath(), image_photo, options);
            }
        } else {
            image_photo.setVisibility(View.GONE);
            image_add.setVisibility(View.VISIBLE);
        }

        if (status.equals(COMPLETED)) {
            image_photo.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    return true;
                }
            });
            image_add.setEnabled(false);
        }

        return view;
    }

    private class MyOnLongClickListener implements View.OnLongClickListener {
        int selectPosition;

        public MyOnLongClickListener(int position) {
            this.selectPosition = position;
        }

        @Override
        public boolean onLongClick(View v) {

            new AlertView("请选择", null, "取消", null, new String[]{"重新拍摄", "删除"},
                    context, AlertView.Style.ActionSheet, new OnItemClickListener() {
                public void onItemClick(Object o, int position) {
                    if (position == 0) {
                        takePhoto(CAMERA_FEEDBACK_RESHOT + selectPosition);
                    } else if (position == 1) {
                        DataSupport.deleteAll(SelectListBean.class, "imagePath = ? OR value = ?", selectListBeanList.get(selectPosition).getImagePath(), selectListBeanList.get(selectPosition).getValue());
                        selectListBeanList.remove(selectPosition);
                        notifyDataSetChanged();
                    }
                }
            }).show();

            return true;
        }
    }

    private class MyOnClickListener implements View.OnClickListener {
        int selectPosition;

        public MyOnClickListener(int selectPosition) {
            this.selectPosition = selectPosition;
        }

        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.image_photo:
                    Intent intent = new Intent(context, PhotoShowBigActivity.class);
                    if (selectListBeanList.get(selectPosition).getValue() != null && !selectListBeanList.get(selectPosition).getValue().equals("")) {
                        intent.putExtra("ImagePath", imageLoader.getDiscCache().get(selectListBeanList.get(selectPosition).getValue()).getPath());
                    } else if (selectListBeanList.get(selectPosition).getImagePath() != null) {
                        intent.putExtra("ImagePath", selectListBeanList.get(selectPosition).getImagePath());
                    }
                    context.startActivity(intent);
                    break;
                case R.id.image_add:
                    takePhoto(CAMERA_FEEDBACK_ADD);
                    break;
            }

        }
    }

    private void takePhoto(int backType) {
        String photoName = UUID.randomUUID().toString();
        Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
        File photo = new File(Environment.getExternalStorageDirectory() + "/DCIM/Camera/", photoName + ".jpeg");
        intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photo));
        ((Activity) context).startActivityForResult(intent, backType);
        EventBus.getDefault().post(new EventCenter.GetImagePath(photo.getPath(), feedBackPosition));
    }
}
